import * as BABYLON from "babylonjs";
import { Vector3 } from "babylonjs";

export class Light3D {
    private _canvas: HTMLCanvasElement;
    private _engine: BABYLON.Engine;
    private _scene: BABYLON.Scene;
    private _camera: BABYLON.ArcRotateCamera;
    private _light: BABYLON.Light;
    constructor(canvasId: string) {
        this._canvas = document.getElementById(canvasId) as HTMLCanvasElement;
        this._engine = new BABYLON.Engine(this._canvas, true);
        this._scene = {} as BABYLON.Scene;
        this._camera = {} as BABYLON.ArcRotateCamera;
        this._light = {} as BABYLON.Light;
    }

    createScene(): void {
        this._scene = new BABYLON.Scene(this._engine);
        this._scene.ambientColor = new BABYLON.Color3(1, 1, 1); // 设置背景颜色
        this._camera = new BABYLON.ArcRotateCamera('arccamera', 0, 0, 10, new BABYLON.Vector3(0, 0, 0), this._scene);
        this._camera.position = new BABYLON.Vector3(0, 0, 20);
        this._camera.attachControl(this._canvas, true);

        // 光源最大限制为4个
        // 1. 点光源 灯泡
        // let lightPoint = new BABYLON.PointLight('pointlight', new BABYLON.Vector3(-5, 10, -5),this._scene);

        // 2. 平衡光 太阳
        // let lightDirection = new BABYLON.DirectionalLight('dlight', new BABYLON.Vector3(10, -1, 0), this._scene);

        // 3. 聚光灯
        // let lightSplot = new BABYLON.SpotLight('splotlight', new BABYLON.Vector3(0, 30, -10), new BABYLON.Vector3(0, -5, 0), Math.PI / 3, 2, this._scene);

        // 4. 半球光 环境光
        // let hlight = new BABYLON.HemisphericLight('hlight', new Vector3(1, 2, 0), this._scene);

        // 5. 光颜色
        // let light = new BABYLON.PointLight('color', new BABYLON.Vector3(0, 10, 0), this._scene);
        // light.diffuse = BABYLON.Color3.Red(); //漫反射，基本光颜色
        // light.specular = BABYLON.Color3.Green(); //镜面反射 高亮点
        // // light.groundColor // 仅适用于半球光，显示为地面的光颜色

        // 6. 投影纹理
        let light = new BABYLON.SpotLight('spot', new BABYLON.Vector3(0, 30, 0), new BABYLON.Vector3(0, -5, 0), Math.PI / 3, 2, this._scene);
        light.projectionTexture = new BABYLON.Texture('images/bg8.png', this._scene);

        // this._light = new BABYLON.HemisphericLight('mylight', new BABYLON.Vector3(0, 1, 0), this._scene);
    }

    createShape(): void {
        let sphere = BABYLON.MeshBuilder.CreateSphere('mysphere', {}, this._scene);
        sphere.scaling = new BABYLON.Vector3(3, 3, 3);
    }

    doRender(): void {
        // Run the render loop.
        this._engine.runRenderLoop(() => {
            this._scene.render();
        });

        // The canvas/window resize event handler.
        window.addEventListener('resize', () => {
            this._engine.resize();
        });
    }
}